package com.authine.cloudpivot.ext.service;

import com.authine.cloudpivot.ext.dto.kingDee.request.*;
import com.authine.cloudpivot.ext.dto.voucher.KingDeeBasicStats;
import com.authine.cloudpivot.ext.enums.kingdee.KingDeeBasicStatsEnum;
import com.authine.cloudpivot.ext.kingdee.work.KingDeeBasicStatsWork;
import com.authine.cloudpivot.ext.kingdee.work.OrganizationDataWork;
import com.authine.hermes.app.launcher.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

/**
 * @Author: liwei
 * @DateTime: 2021/7/16
 * @Description: TODO
 */
@Slf4j
public class OrganizationDataService {

    OrganizationDataWork organizationDataWork = new OrganizationDataWork();
    KingDeeBasicStatsWork kingDeeBasicStatsWork = new KingDeeBasicStatsWork();


    /**
     * 创建员工
     * @param kingDeeMember
     * @return
     */
    public Response createMember(KingDeeMember kingDeeMember) {
        Assert.notNull(kingDeeMember, "创建员工 入参数据为空");

        return organizationDataWork.createMember(kingDeeMember);
    }


    /**
     * 创建部门
     * @param kingDeeMember
     * @return
     */
    public Response createDepartment(KingDeeMember kingDeeMember) {
        Assert.notNull(kingDeeMember, "创建部门 入参数据为空");

        return organizationDataWork.createDepartment(kingDeeMember);
    }

    /**
     * 创建组织
     * @param org
     * @return
     */
    public Response createOrganization(KingDeeOrganization org) {

        Assert.notNull(org, "创建组织 入参数据为空");
        KingDeeBasicStatsEnum type = org.getVendorAndCustomer()?KingDeeBasicStatsEnum.Customer:KingDeeBasicStatsEnum.Vendor;
        KingDeeBasicStats param = new KingDeeBasicStats();
        param.setKingdee_code(org.getId());
        param.setCompany_name(org.getCompanyName());
        param.setInfo_type(type);

        Boolean checkResult = kingDeeBasicStatsWork.checkCodeAndNameExist(param.getKingdee_code(), param.getCompany_name(), param.getInfo_type());

        Assert.isTrue(!checkResult,"保存失败：" + param.getCompany_name() + " 名称 " + param.getKingdee_code() + " 编码在 " + param.getInfo_type() + "中已存在!");

        String id = kingDeeBasicStatsWork.save(param);
        log.info("金蝶基础数据实体 id:{}  ##################", id);

        KingDeeOrganization kingDeeOrganization = KingDeeOrganization.builder()
                .id(param.getKingdee_code())
                .companyName(param.getCompany_name())
                .vendorAndCustomer(param.getInfo_type().equals(KingDeeBasicStatsEnum.Customer))
                .build();
        return organizationDataWork.createOrganization(org);
    }

    /**
     * 创建转采供应商
     * @param vendor
     * @return
     */
    public Response createTransferVendor(KingDeeTransferVendor vendor) {
        Assert.notNull(vendor, "创建转采供应商 入参数据为空");

        KingDeeBasicStatsEnum type = KingDeeBasicStatsEnum.Vendor;
        KingDeeBasicStats param = new KingDeeBasicStats();
        param.setKingdee_code(vendor.getId());
        param.setCompany_name(vendor.getCompanyName());
        param.setInfo_type(type);

        Boolean checkResult = kingDeeBasicStatsWork.checkCodeAndNameExist(param.getKingdee_code(), param.getCompany_name(), param.getInfo_type());

        Assert.isTrue(!checkResult,"保存失败：" + param.getCompany_name() + " 名称 " + param.getKingdee_code() + " 编码在 " + param.getInfo_type() + "中已存在!");

        String id = kingDeeBasicStatsWork.save(param);
        log.info("金蝶基础数据实体 id:{}  ##################", id);
        return organizationDataWork.createTransferVendor(vendor);
    }

}
